import { Injectable } from '@angular/core';
import {
  HttpEvent, HttpInterceptor, HttpHandler, HttpRequest
} from '@angular/common/http';

import { Observable } from 'rxjs';

/** Pass untouched request through to the next request handler. */
@Injectable()
export class GlobalNoopInterceptor  implements HttpInterceptor {

  intercept(req: HttpRequest<any>, next: HttpHandler):
    Observable<HttpEvent<any>> {
    console.log("所有请求都会经过这里")
    const token = window.localStorage.getItem('auth_token')
    const authReq = req.clone({
      headers: req.headers.set('X-Access-Token', token),
    });
    // 获取请求类型
    //当请求类型为登录或者注册时，不用返回请求头，避免登录或者注册失败
    var urlrouter = location.href.substr(location.href.lastIndexOf('/')+1)
    console.log(urlrouter)
    if(urlrouter == 'sigin' || urlrouter =='siginup') {
      return next.handle(req);
    } else {
      return next.handle(authReq);
    }

  }
}